//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CFont9.cpp>
///	@path	~/src/graphics/dx9/
///	@date	2007/11/09
///	@desc	DirectX 9 fonts implementation.

#include "config/config.h"

#include "lib/system/thread_debug.h"

#include "graphics/DxHelpers.h"
#include "graphics/dx9/CFont9.h"
#include "graphics/dx9/CSprite9.h"

namespace xeres {

	IMPL_IOBJECT_CLASS( CFont9 );

	// ctor
	CFont9::CFont9( ID3DXFont * pFont )
		: m_pFont( pFont )
	{
	}

	// dtor
	CFont9::~CFont9( void )
	{
		if( m_pFont )
		{
			UINT rest = m_pFont->Release();
			if( rest != 0 )
			{
				TRACE_ERROR( _S("CFont9::DTOR: Invalid ID3DXFont reference count.") );
				FATAL_ABORT( _S("Invalid ID3DXFont reference count.") );
			}
		}
	}
	
	// on lost device
	bool CFont9::OnLostDevice( void )
	{
		TRACK_FUNCTION_SCOPE();
		RUNNING_ON_THREAD( _S("device") );

		if( m_pFont )
		{
			HRESULT hr = m_pFont->OnLostDevice();
			return SUCCEEDED( hr );
		}
		return true;
	}

	// on reset device
	bool CFont9::OnResetDevice( RefWeak<IDevice> device )
	{
		TRACK_FUNCTION_SCOPE();
		RUNNING_ON_THREAD( _S("device") );

		if( m_pFont )
		{
			HRESULT hr = m_pFont->OnResetDevice();
			return SUCCEEDED( hr );
		}
		return true;
	}

	// draw text
	int CFont9::DrawText( RefWeak<ISprite> sprite , const WString& text ,
			int rect[4] , int style , PackColor4 color )
	{
		RefWeak<CSprite9> sprite9( sprite );

		return m_pFont->DrawText(
			sprite9->GetSprite() ,
			text.c_str() , (UINT)text.size() , 
			(LPRECT)rect ,
			style , color.get_packed_argb() );
	}

	int CFont9::DrawTextX( ID3DXSprite * D3DX_sprite , const WString& text ,
			int rect[4] , int style , DWORD color )
	{
		return m_pFont->DrawText(
			D3DX_sprite ,
			text.c_str() , (UINT)text.size() , 
			(LPRECT)rect ,
			style , color );
	}

	// GetDC
	void * CFont9::GetDC( void )
	{
		return m_pFont->GetDC();
	}

} // namespace xeres
